### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")
mydata <- read.dta("Analysis Data/NIBRS_analysis.dta")
mydata_donut <- read.dta("Analysis Data/NIBRS_analysis_donut.dta")

column_1 <- feols(homicide_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(column_1, cluster="STATE")
column_2 <- feols(homicide_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata_donut)
summary(column_2, cluster="STATE")
column_3 <- feols(homicide_pc~temp_MP + prec_MP | state_sample_month+STATE[tMean]+sample_month[tMean]+STATE[prec]+sample_month[prec] + week + dow, data=mydata)
summary(column_3, cluster="STATE")
column_4 <- feols(homicide_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(column_4, cluster="STATE")

models <- list(column_1, column_2, column_3, column_4)
modelsummary(models, vcov = ~STATE, output = "Figures and Tables/Table_3/Columns_1_4.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))
